/**
 * @copyright Copyright (c) 2025 CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author Xie Jinhui
 */

#include "main.h"

#define SOFTWAREVERSION "2.0.3"
/**
 * @brief help
 *
 */
static void help(void)
{
	printf("temperature{ -h | -t | -v }\n\
-h	--help\n\
-t	--temperature\n\
-v	--version\n");
}

/**
 * @brief Get the temperature object
 *
 * @return double
 */
static int get_temperature(double * temp)
{
	char buf[64] = {0};
	char file_dir[128] = {0};
	FILE *fp;

	/* D9温度文件路径："/sys/class/thermal/thermal_zone0/temp" */
	strcpy(file_dir, TEMPER_CONFIG_D9);
	fp = fopen(file_dir, "r");
	if (fp != NULL)
	{
		fgets(buf, sizeof(buf), fp);
		if (buf[0] == '-')
		{
			*temp = atof(buf + 1) / (-1000);
		}
		else
		{
			*temp = atof(buf) / 1000;
		}	
	}
	else
	{
		printf("ERROR: failed to open %s.\n", file_dir);
		return -1;
	}

	fclose(fp);

	return 0;
}


/**
 * @brief Get the Option Param object
 *
 * @param argc
 * @param argv
 * @return uint8_t
 */
uint8_t getOptionParam(int argc, char **argv)
{
	int ch;
	double temp = 0;

	ch = getopt(argc, argv, "htv");
	if (ch == -1)
	{
	    printf("Command parameter error!\n");
		help();
		return FALSE;
	}

	switch (ch)
	{
	case 'h':
		help();
		break;

	case 't':
		if( get_temperature(&temp) == 0){
			printf("Main board: %0.2f °C\n", temp);
		}else{
			return -1;
		}
		break;

	case 'v':
		printf("version: V%s\n", SOFTWAREVERSION);
		break;

	default:
		help();
		break;
	}


	return TRUE;
}
